import { defineStore } from 'pinia';
import { ref, watch, reactive } from 'vue';
import { getTasks, stagingMemo, fulfilMemo, finishTask } from '@/api/sting/sting';
import { ResultEnum } from '@/enums/httpEnum';

interface Tasks {
  content: string;
  finish_time: string;
  found_time: string;
  id: number;
  personId: number;
  title: string;
}
interface Memos {
  content: string;
  finish_time: string;
  found_time: string;
  id: number;
  personId: number;
  title: string;
}
//获取任务信息
export const taskStore = defineStore('task', () => {
  const task = ref<Tasks[]>([]);
  const limit = 3,
    offset = ref(0),
    count = ref(0),
    max = ref(0),
    page = ref(1),
    isEnd = ref(false),
    conditions = reactive({
      days: 'all',
      task: 'all',
    });
  // 查询数据
  const search = async () => {
    // 如果已经加载到最后一页，则放弃发起请求。
    if (isEnd.value) return;
    const data = await getTasks({
      limit,
      offset: (page.value - 1) * limit,
      ...conditions,
    });
    const { result } = data;
    // console.log(result);
    // console.log(data);
    if (data.code == ResultEnum.SUCCESS) {
      count.value = result.count;
      isEnd.value = page.value >= max.value;
      page.value = result.page;
      max.value = Math.ceil(count.value / limit);
      task.value = result.results;
    }
  };

  watch(
    conditions,
    () => {
      task.value = [];
      page.value = 1;
      count.value = 0;
      search();
    },
    { deep: true }
  );
  // 完成任务
  const finishTasked = async (id: number) => {
    finishTask(id).then((res) => {
      const { result } = res;
      console.log(result);
      if (res.code == ResultEnum.SUCCESS) {
        task.value = task.value.map((t) => (t.id == result.id ? result : t));
        search();
      }
    });
  };
  return {
    task,
    limit,
    offset,
    conditions,
    search,
    page,
    max,
    count,
    isEnd,
    finishTasked,
  };
});
//获取标签
export const memoStore = defineStore('memo', () => {
  const memos = ref<Memos[]>([]);
  const limit = 3,
    offset = ref(0),
    count = ref(0),
    max = ref(0),
    page = ref(1),
    conditions = reactive({
      days: 'all',
      memo: 'all',
    });
  // 查询数据
  const searches = async () => {
    const data = await stagingMemo({
      limit,
      offset: (page.value - 1) * limit,
      ...conditions,
    });
    const { result } = data;
    // console.log(result);
    // console.log(data);
    if (data.code == ResultEnum.SUCCESS) {
      count.value = result.count;
      page.value = result.page;
      max.value = Math.ceil(count.value / limit);
      memos.value = result.results;
    }
  };

  watch(
    conditions,
    () => {
      memos.value = [];
      page.value = 1;
      count.value = 0;
      searches();
    },
    { deep: true }
  ); //  * 完成便签
  const finishMemo = async (id: number) => {
    fulfilMemo(id).then((res) => {
      const { result } = res;
      console.log(result);

      if (res.code == ResultEnum.SUCCESS) {
        memos.value = memos.value.map((t) => (t.id == result.id ? result : t));
        searches();
      }
    });
  };
  return {
    memos,
    limit,
    offset,
    conditions,
    searches,
    page,
    max,
    count,
    finishMemo,
  };
});
